package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.repeat.NoRepeatSubmit;
import com.qiguliuxing.dts.db.bean.DtsBrandMerchantInfo;
import com.qiguliuxing.dts.db.bean.DtsBrandMerchantVo;
import com.qiguliuxing.dts.db.bean.DtsMerchantApplyInfo;
import com.qiguliuxing.dts.db.domain.DtsAdmin;
import com.qiguliuxing.dts.db.domain.DtsBrandMerchant;
import com.qiguliuxing.dts.merchant.service.MerchantAdminService;
import com.qiguliuxing.dts.merchant.service.MerchantApplyService;
import com.qiguliuxing.dts.service.base.DtsBankSupportService;
import com.qiguliuxing.dts.service.base.DtsBrandService;
import com.qiguliuxing.dts.service.base.DtsMerchantWithdrawalService;
import com.qiguliuxing.dts.service.notify.NotifyService;
import com.qiguliuxing.dts.service.qcode.QCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/merchant/apply")
@Validated
@Api(tags= ApiAdminConsts.Storage.PARENT)
@Slf4j
public class MerchantApplyController {

	@Autowired
	private MerchantApplyService merchantApplyService;

	@Autowired
	private MerchantAdminService merchantAdminService;

	@Autowired
	private DtsBrandService brandService;

	@Autowired
	private QCodeService qCodeService;

	@Autowired
	private DtsBankSupportService bankSupportService;

	@Autowired
	private NotifyService notifyService;

	@Autowired
	private DtsMerchantWithdrawalService dtsMerchantWithdrawalService;

	@NoRepeatSubmit
	@PostMapping("/commit")
	@ApiOperation(value = "商户入驻申请")
	public Object commit(@RequestBody DtsMerchantApplyInfo merchantApplyInfo){
		DtsAdmin existMailAdminUser = merchantAdminService.findMerchantAdminByEmail(merchantApplyInfo.getEmail());

		if(Objects.nonNull(existMailAdminUser)){
			if(!existMailAdminUser.getDeleted()){
				//该邮箱申请过且通过
				return ResponseUtils.fail(302,"该邮箱已申请入驻,且已审核通过");
			}else{
				//之前申请过的
				/*更新后台登录信息*/
				merchantAdminService.updateAdminUserInfo(merchantApplyInfo,existMailAdminUser);
				/*更新申请店铺信息*/
				merchantApplyService.updateMerchantApply(merchantApplyInfo,existMailAdminUser.getId());
			}
		}else{
			DtsAdmin existAdminUser = merchantAdminService.findAllMerchantAdmin(merchantApplyInfo.getUsername());
			if(Objects.nonNull(existAdminUser)){
				return ResponseUtils.fail(301,"该用户已被注册");
			}
			/*存储后台登录信息*/
			merchantAdminService.saveAdminUserInfo(merchantApplyInfo);
			/*存储申请店铺信息*/
			merchantApplyService.addMerchantApply(merchantApplyInfo);
		}
		notifyService.notifySslMail("商户入驻审核通知","恭喜您！商户入驻申请提交成功！审核时间一般为1-2个工作日，请耐心等待邮件通知！",new String[]{merchantApplyInfo.getEmail()});
		return ResponseUtils.ok();
	}

	@PostMapping("/merchantClass")
	@ApiOperation(value = "商户主营类目")
	public Object merchantClass(){
		return ResponseUtils.ok(merchantApplyService.queryAllMerchantCategory());
	}

	@PostMapping("/applyConfig")
	@ApiOperation(value = "营业执照示例和平台协议配置信息")
	public Object applyConfig(){
		return ResponseUtils.ok(merchantApplyService.applyConfig());
	}

	@PostMapping("/merchantBrandInfo")
	@ApiOperation(value = "商户品牌信息查询")
	public Object merchantBrandInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		DtsAdmin admin = (DtsAdmin)currentUser.getPrincipal();
		DtsBrandMerchantVo vo = new DtsBrandMerchantVo();
		DtsBrandMerchant brandMerchant = brandService.findById(admin.getBrandId());
		BeanUtils.copyProperties(brandMerchant,vo);
		//查询转账信息
		vo.setWithdrawAccount(dtsMerchantWithdrawalService.findByBrandId(brandMerchant.getId()));
		return ResponseUtils.ok(vo);
	}

	@PostMapping("/supportBank")
	@ApiOperation(value = "支持的提现行查询")
	public Object supportBank(){
		return ResponseUtils.ok(bankSupportService.queryAll());
	}

	@PostMapping("/updateMerchantBrandInfo")
	@ApiOperation(value = "商户品牌信息完善")
	public Object updateMerchantBrandInfo(@RequestBody DtsBrandMerchantInfo brand){
		DtsBrandMerchant validBrand = new DtsBrandMerchant();
		BeanUtils.copyProperties(brand,validBrand);
		if (!brandService.validate(validBrand)) {
			return ResponseUtils.badArgumentValue();
		}
		try {
			//生成店铺的分享URL
			String defaultCategory = brandService.getBrandCategory(brand.getDefaultCategoryId());
			String shareUrl = qCodeService.createBrandImage(null,brand.getId(), brand.getPicUrl(), brand.getName(),defaultCategory);
			brand.setShareUrl(shareUrl);
		} catch (Exception e) {
			log.error("生成品牌商铺分享图URL出错：{}",e.getMessage());
			e.printStackTrace();
		}
		new Thread(()-> brandService.updateBrandAndWithdrawalInfo(brand)).start();
		return ResponseUtils.ok(brand);
	}

}
